//
//  LXMineViewController.m
//  bgms_d
//
//  Created by lachesis-zhu on 03/07/2017.
//  Copyright © 2017 lachesis-zhu. All rights reserved.
//

#import "LXMineViewController.h"
#import "DVPieChart.h"
#import "DVFoodPieModel.h"

@interface LXMineViewController ()

@property (nonatomic, strong) DVPieChart *chart;

@end

@implementation LXMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addNotification];
    [self refetchBattle];
    [self initBettleView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)refetchBattle {
    UInt8 data[3];
    data[0] = 0x05;
    data[1] = 0x01;
    data[2] = 0x00;
    [[BLETransDeviceSingleton sharedInstance] characteristc:COMMON_CHARACTERISTIC_UUID sendHostCmd:data lenght:3];
}
#pragma mark -  notification
- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDataReceive:) name:COMMON_CHARACTERISTIC_UUID object:nil];
}

-(void)bleDataReceive:(NSNotification *)notification
{
    NSData *data = notification.userInfo[kAppHeadUnitData];
    Byte *MessageBt = (Byte *)[data bytes];
    if (MessageBt[0] == 5) {
        NSInteger comandStatus = MessageBt[2];
        CGFloat value = [self getBattlePercent:comandStatus];
        [self showBettleView:value];
    }
}

- (CGFloat)getBattlePercent:(NSInteger)index
{
    CGFloat percent;
    switch (index) {
        case 1:
            percent = 0.05;
            break;
        case 2:
            percent = 0.15;
            break;
        case 3:
            percent = 0.2;
            break;
        case 4:
            percent = 0.3;
            break;
        case 5:
            percent = 0.4;
            break;
        case 6:
            percent = 0.5;
            break;
        case 7:
            percent = 0.6;
            break;
        case 8:
            percent = 0.7;
            break;
        case 9:
            percent = 0.8;
            break;
        case 10:
            percent = 0.9;
            break;
        case 11:
            percent = 0.95;
            break;
        case 12:
            percent = 1;
            break;
        default:
            percent = 1;
            break;
    }
    return percent;
}

- (void)initBettleView {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    self.chart = [[DVPieChart alloc] initWithFrame:CGRectMake(0, 20, width, 480)];
    
    [self.view addSubview:self.chart];
    
    DVFoodPieModel *model1 = [[DVFoodPieModel alloc] init];
    model1.rate = 0.0833;
    
    DVFoodPieModel *model2 = [[DVFoodPieModel alloc] init];
    model2.rate = 0.0833;
    
    DVFoodPieModel *model3 = [[DVFoodPieModel alloc] init];
    model3.rate = 0.0833;
    
    DVFoodPieModel *model4 = [[DVFoodPieModel alloc] init];
    model4.rate = 0.0833;
    
    DVFoodPieModel *model5 = [[DVFoodPieModel alloc] init];
    model5.rate = 0.0833;
    
    DVFoodPieModel *model6 = [[DVFoodPieModel alloc] init];
    model6.rate = 0.0833;
    
    DVFoodPieModel *model11 = [[DVFoodPieModel alloc] init];
    model11.rate = 0.0833;
    
    DVFoodPieModel *model12 = [[DVFoodPieModel alloc] init];
    model12.rate = 0.0833;
    
    DVFoodPieModel *model13 = [[DVFoodPieModel alloc] init];
    model13.rate = 0.0833;
    
    DVFoodPieModel *model14 = [[DVFoodPieModel alloc] init];
    model14.rate = 0.0833;
    
    DVFoodPieModel *model15 = [[DVFoodPieModel alloc] init];
    model15.rate = 0.0833;
    
    DVFoodPieModel *model16 = [[DVFoodPieModel alloc] init];
    model16.rate = 0.0833;
    
    NSArray *dataArray = @[model1, model2, model3, model4, model5, model6,
                           model11, model12, model13, model14, model15, model16];
    
    self.chart.dataArray = dataArray;
    self.chart.title = [NSString stringWithFormat:@"%@", @"%"];
    
    [self showBettleView:0];
}

- (void)showBettleView:(CGFloat)value {
    self.chart.battlePercent = value;
}
@end
